<script type="text/ecmascript-6">
  import Navbar from '../../components/base/navbar.vue';
  import Popup from '../../components/base/mask.vue';
  import pageCouponSel from '../../projects/dmp/pages/coupon/select';
  import pageRegisterSuc from '../../projects/dmp/pages/registration/register_success.vue';
  export default{
      name:"registration_register",
      components:{
          Navbar,
          Popup,
          pageCouponSel,
          pageRegisterSuc
      },
      data(){
         return{
           myID: this.$route.params.id,
           couponChecked: [],
           couponSelected: {},
           showPageCouponSel: false,
           showPageRegisterSuc: false,
           couponChecked: [],
           couponSelected: {},
           isList: false,
           isNoCoupon: false
         }
      },
      created(){
          this.$store.dispatch("queryRegister",this.myID);
          const page = this.$route.query.page;
          if(page === 'sel_coupon'){
             this.showPageCouponSel=true;
          }
        EventBus.$on("on-change",(flag)=>{
             this.showPageCouponSel=flag;
          });
        EventBus.$on("on-checked-change",(val)=>{
             this.couponChecked=val;
        });
        EventBus.$on('on-selected-change',(val)=>{
             this.couponSelected=val;
        })
        EventBus.$on('register-complete',this.registerComplete);
      },
       methods:{
         editInfo(){
           const host = window.location.host,
             protocol = window.location.protocol,
             url = encodeURIComponent(window.location.href);
             window.location.href = protocol + '//' + host + '#!/user/profile' + '?from=' + url;
         },
         selCoupon() {
           if(this.isNoCoupon) return;
           function isEmptyObject(obj) {
             var t;
             for (t in obj)
               return !1;
             return !0;
           }

           this.couponChecked = [];
           if (!isEmptyObject(this.couponSelected)) {
             const index = this.couponSelected.index;
             this.couponChecked.push(this.sign.coupon[index]);
           }
           this.showPageCouponSel = true;
         },
         subRegister(){
           if(this.isNoCoupon){
               this.$Modal.info({title:"消息提示",body:"您没有可用的野练PASS，无法报名!"});
               return;
           }
           if (this.sign.size != null && this.sign.size.upper && this.sign.size.lower && this.sign.size.shoes){
               const coupon_id =this.couponSelected.id;
               if(coupon_id){
                   this.$store.dispatch("submitRegister",{
                       id:this.myID,
                       coupon_id:coupon_id
                   });
               }else{
                 this.$Modal.info({title:"消息提示",body:"请选择要使用的野练PASS！"});
               }
           }else{
               this.$Modal.info({title:"消息提示",body:"请先完善报名资料中的服装尺码信息！"});
           }
         },
         registerComplete(res) {
           switch (res.data.pay_status) {
             case 0:
               //待付款
               break;
             case 1:
               this.showPageRegisterSuc = true;
               break;
           }
         }
       },
      beforeDestory(){
          EventBus.$off("on-change");
          EventBus.$off("on-checked-change");
          EventBus.$off("on-selected-change");
          EventBus.$off("register-complete");
      },
      computed:{
        couponSelTxt: function () {
          if (this.couponSelected.discount) {
            return this.couponSelected.discount.title;
          } else {
            switch (this.sign.coupon.length) {
              default:
                let ends = [];

                this.sign.coupon.forEach(function(v){
                  ends.push(Date.parse(v.ends_at.replace(/-/g,"/")));
                });
                const minCoupon = Math.min.apply(null, ends),
                  minIndex = ends.indexOf(minCoupon);
                this.couponChecked.push(this.sign.coupon[minIndex]);
                this.couponSelected = this.couponChecked[0];
                return this.couponSelected.discount.title;
              case 0:
                this.isNoCoupon = true;
                return '您没有可用的野练PASS';
              case 1:
                this.couponChecked.push(this.sign.coupon[0]);
                this.couponSelected = this.couponChecked[0];
                return this.couponSelected.discount.title;

            }
          }

        },
          sign:{
              get(){
                  return this.$store.getters.getRegister
              }
          }
      }
  }
</script>
